package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.dao.PurchaseDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.PurchaseService;
import com.atguigu.jxc.util.StringUtil;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.gson.JsonObject;
import jdk.nashorn.internal.ir.CallNode;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: dongdong lau
 * @CreateTime: 2021/7/23 14:50
 * @Description: 新增收货单实现类
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class PurchaseServiceImpl implements PurchaseService {

    @Autowired
    private PurchaseDao purchaseDao;

    @Autowired
    private UserDao userDao;


    @Override
    public ServiceVO save(PurchaseList purchaseList, String purchaseListGoodsStr) {
        // 查询当前用户信息
        User user = userDao.findUserByName((String)SecurityUtils.getSubject().getPrincipal());
        purchaseList.setUserId(user.getUserId());
        // 对传入的数据进行判空
        if (purchaseList == null){
            return new ServiceVO<>(ErrorCode.NULL_POINTER_CODE, ErrorCode.NULL_POINTER_MESS);
        }
        purchaseDao.savePurchasePurchaseList(purchaseList);

        if(StringUtil.isEmpty(purchaseListGoodsStr)){
            return new ServiceVO<>(ErrorCode.NULL_POINTER_CODE, ErrorCode.NULL_POINTER_MESS);
        }
        // 将purchaseListGoodsStr转换PurchaseListGood类
//        purchaseListGoodsStr = purchaseListGoodsStr.replace("[","").replace("]","");
        List<PurchaseListGoods> purchaseListGoods = JSON.parseArray(purchaseListGoodsStr, PurchaseListGoods.class);
        // 遍历purchaseListGoods
        for (PurchaseListGoods purchaseListGood : purchaseListGoods) {
            // 将purchaseListId设置进purcharseListGoods
            Integer purchaseListId = purchaseList.getPurchaseListId();
            purchaseListGood.setPurchaseListId(purchaseListId);
            purchaseDao.savePurchasePurchaseListGoods(purchaseListGood);
            // 加库存
            Integer goodsId = purchaseListGood.getGoodsId();
            Integer goodsNum = purchaseListGood.getGoodsNum();
            purchaseDao.reduceGoods(goodsId, goodsNum);
        }


        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 进货单列表展示
     * @param purchaseNumber 单据号
     * @param supplierId 供应商id
     * @param state 支付状态
     * @param sTime 起始时间
     * @param eTime 结束时间
     * @return
     */
    @Override
    public Map<String, Object> list(String purchaseNumber,
                                    Integer supplierId,
                                    Integer state,
                                    String sTime,
                                    String eTime) {
        // 定义返回参数
        Map<String, Object> map = new HashMap<>();
        // 获取数据
        List<PurchaseList> list = purchaseDao.list(purchaseNumber, supplierId, state, sTime, eTime);
        // 向返回参数中添加数据
        map.put("rows", list);
        // 新增注释
        // 返回数据
        return map;
    }

    /**
     * 查询商品信息
     * @param purchaseListId
     * @return
     */
    @Override
    public Map<String, Object> getPurchaseListGoodsById(Integer purchaseListId) {
        // 初始化返回列表
        Map<String, Object> map = new HashMap<>();
        // 对传入的id进行判空
        if(purchaseListId == null){
            throw new RuntimeException("参数不能为空");
        }
        // 调用接口获取数据
        List<PurchaseListGoods> purchaseListGoodsById = purchaseDao.getPurchaseListGoodsById(purchaseListId);
        // 设置返回参数
        map.put("rows", purchaseListGoodsById);
        // 返回数据
        return map;
    }

    /**
     * 根据purchaseListId删除进货单信息
     * @param purchaseListId
     * @return
     */
    @Override
    public ServiceVO deletePurchaseListById(Integer purchaseListId) {
        // 调用删除mapper层接口
        Integer result = purchaseDao.deletePurchaseListById(purchaseListId);
        return new ServiceVO<SuccessCode>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }


    public static void main(String[] args) {

    }
}
